package com.itheima.test;

import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


@SpringBootTest
@RunWith(SpringRunner.class)
public class TextMinio {
    @Test
    public void test() throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 1. 创建minio的客户端
        MinioClient client = MinioClient.builder()
                .endpoint("http://localhost:9000")
                .credentials("minioadmin", "minioadmin")
                .build();
        // 2. 判断是否有某个桶,没有的话创建一个  bucketExists 判断是否存在
        boolean exists = client.bucketExists(BucketExistsArgs.builder().bucket("crm001").build());
        if (exists){
            System.out.println("桶已经存在");
        }else {
            client.makeBucket(MakeBucketArgs.builder().bucket("crm001").build());
        }
        // 3. 上传文件
        // 方式1
//        client.uploadObject(UploadObjectArgs.builder()
//                .bucket("crm001")
//                .object("1.jpg")
//                .filename("C:\\Users\\333\\Desktop\\1.jpg")
//                .contentType("image/jpg")
//                .build());
        //方式2
        File file = new File("C:\\Users\\333\\Desktop\\2.png");
        FileInputStream fileInputStream = new FileInputStream(file);
        client.putObject(PutObjectArgs.builder()
                .bucket("crm001")
                .object("2.png")
                .contentType("image/png")
                .stream(fileInputStream,file.length(),-1)
                .build());
    }


}
